package com.shenmazong.servereduresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.servereduresume.dto.TbEducation;
import com.shenmazong.servereduresume.dto.TbInformation;
import com.shenmazong.servereduresume.mapper.TbInformationMapper;
import com.shenmazong.servereduresume.service.TbInformationService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbInformationServiceImpl extends ServiceImpl<TbInformationMapper, TbInformation>
implements TbInformationService {
    @Autowired
    private TbInformationMapper tbInformationMapper;
    @Override
    public ResultResponse findByUserIdInformationId(BatchChooseIdVo batchChooseIdVo) {
        TbInformation tbInformation = null;
        try {
            Object informationId = batchChooseIdVo.getParamMap().get("informationId");
            Object userId = batchChooseIdVo.getParamMap().get("userId");
            if (informationId==null){
                return ResultResponse.FAILED("非法参数");
            }
            if (userId==null){
                return ResultResponse.FAILED("非法参数");
            }
            tbInformation = tbInformationMapper.selectOne(new QueryWrapper<TbInformation>().lambda()
                    .eq(TbInformation::getUserId, userId).eq(TbInformation::getDeleteFlag, 0)
                    .eq(TbInformation::getInformationId, informationId).last("limit 1"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS(tbInformation);
    }

    @Override
    public ResultResponse saveInformation(TbInformation tbInformation) {
        try {
            tbInformationMapper.insert(tbInformation);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateInformation(TbInformation tbInformation) {
        try {
            tbInformationMapper.updateById(tbInformation);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse deleteInformation(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()==0){
            return ResultResponse.FAILED("至少选择一个删除");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                TbInformation certificate = tbInformationMapper.selectById(id);
                certificate.setDeleteFlag((byte) 1);
                tbInformationMapper.updateById(certificate);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }
}




